package com.neusoft.hifly.core.pojo;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.neusoft.hifly.core.log.Log;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * Title: 核心类库
 * <p>
 * Description: Tree 节点数据
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel(value = "TreeNode", description = "树节点")
public class TreeNode implements Serializable, Cloneable, Comparable<TreeNode> {

	private static final long serialVersionUID = -4131682005232664542L;
	// ID
	@ApiModelProperty(value = "ID")
	private String id;
	// 节点类型
	@ApiModelProperty(value = "节点类型")
	private String type;
	// 显示名称
	@ApiModelProperty(value = "显示名称")
	private String label;
	// 名称
	@ApiModelProperty(value = "名称")
	private String name;
	// 排序(降序)
	@ApiModelProperty(value = "排序(降序)")
	private int order;
	// 父节点ID
	@ApiModelProperty(value = "父节点ID")
	private String fatherId;
	// 父节点名称
	@ApiModelProperty(value = "父节点名称")
	private String fatherName;
	// 根节点ID
	@ApiModelProperty(value = "根节点ID")
	private String rootId;
	// 是否可以选择
	@ApiModelProperty(value = "可以选择")
	private boolean check;
	// 是否默认选中
	@ApiModelProperty(value = "默认选中")
	private boolean checked;
	// 是否初始化数据，不可以编辑和删除
	@ApiModelProperty(value = "可以编辑和删除")
	private boolean init;
	// 指定节点是否为叶子节点
	@ApiModelProperty(value = "叶子节点")
	private boolean leaf;
	// 子节点
	@ApiModelProperty(value = "子节点")
	private List<TreeNode> children;

	public TreeNode() {
		super();
	}

	public TreeNode(final String type, final String label) {
		super();
		this.type = type;
		this.label = label;
	}

	public TreeNode(final String type, final String label, final String id, final String name) {
		super();
		this.type = type;
		this.label = label;
		this.id = id;
		this.name = name;
	}

	public TreeNode(final String type, final String label, final String id, final String name, final int order,
			final String fatherId, final String fatherName, final boolean check, final boolean checked,
			final boolean init, final boolean leaf, final List<TreeNode> children) {
		super();
		this.type = type;
		this.label = label;
		this.id = id;
		this.name = name;
		this.order = order;
		this.fatherId = fatherId;
		this.fatherName = fatherName;
		this.check = check;
		this.checked = checked;
		this.init = init;
		this.leaf = leaf;
		this.children = children;
	}

	/**
	 * 克隆对象
	 *
	 * @return 对象
	 */
	@Override
	public TreeNode clone() {
		TreeNode obj = null;
		try {
			obj = (TreeNode) super.clone();
		} catch (final CloneNotSupportedException e) {
			Log.error(e.getMessage(), e);
		}
		return obj;
	}

	/**
	 * 按照排序字段排序
	 *
	 * @param obj
	 *            要比较的对象
	 * @return int 排序
	 */
	@Override
	public int compareTo(final TreeNode obj) {
		if (obj == null) {
			return 0;
		}
		return Integer.valueOf(this.getOrder()).compareTo(Integer.valueOf(obj.getOrder()));
	}

	/**
	 * 得到对象的哈希值
	 *
	 * @return int 对象的哈希值
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.getId()).toHashCode();
	}

	/**
	 * 比较两个对象是否相等
	 *
	 * @param other
	 *            要比较的对象
	 * @return boolean 相同返回true，否则返回flase
	 */
	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof TreeNode)) {
			return false;
		}
		final TreeNode castOther = (TreeNode) other;
		return new EqualsBuilder().append(this.getId(), castOther.getId()).isEquals();
	}

	/**
	 * 返回对象的字符串值
	 *
	 * @return String 表示用户对象的字符串值
	 */
	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}

	public TreeNode id(final String id) {
		this.id = id;
		return this;
	}

	public TreeNode name(final String name) {
		this.name = name;
		return this;
	}

	public TreeNode order(final int order) {
		this.order = order;
		return this;
	}

	public TreeNode rootId(final String rootId) {
		this.rootId = rootId;
		return this;
	}

	public TreeNode fatherId(final String fatherId) {
		this.fatherId = fatherId;
		return this;
	}

	public TreeNode fatherName(final String fatherName) {
		this.fatherName = fatherName;
		return this;
	}

	public TreeNode check(final boolean check) {
		this.check = check;
		return this;
	}

	public TreeNode checked(final boolean checked) {
		this.checked = checked;
		return this;
	}

	public TreeNode init(final boolean init) {
		this.init = init;
		return this;
	}

	public TreeNode leaf() {
		this.leaf = true;
		return this;
	}

	public TreeNode children(final List<TreeNode> children) {
		this.children = children;
		return this;
	}

	public String getType() {
		return type;
	}

	public String getLabel() {
		return label;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getOrder() {
		return order;
	}

	public String getFatherId() {
		return fatherId;
	}

	public String getFatherName() {
		return fatherName;
	}

	public boolean isCheck() {
		return check;
	}

	public boolean isChecked() {
		return checked;
	}

	public boolean isInit() {
		return init;
	}

	public boolean getLeaf() {
		return leaf;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public String getRootId() {
		return rootId;
	}

}
